package me.zhengjie.modules.finace_taxation.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.modules.finace_taxation.domain.FtOrder;
import me.zhengjie.modules.finace_taxation.domain.FtOrderSchedule;
import me.zhengjie.modules.finace_taxation.service.dto.FtServiceDto;
import me.zhengjie.modules.finace_taxation.service.mapper.FtOrderMapper;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.finace_taxation.domain.FtLeaveMessage;
import me.zhengjie.modules.finace_taxation.service.FtLeaveMessageService;
import me.zhengjie.modules.finace_taxation.service.dto.FtLeaveMessageDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtLeaveMessageQueryParam;
import me.zhengjie.modules.finace_taxation.service.mapper.FtLeaveMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @author 杨荣森
* @date 2021-06-30
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FtLeaveMessageService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FtLeaveMessageServiceImpl implements FtLeaveMessageService {

    // private final RedisUtils redisUtils;
    private final FtLeaveMessageMapper ftLeaveMessageMapper;

    @Autowired
    private FtOrderMapper ftOrderMapper;

    @Override
    public PageInfo<FtLeaveMessageDto> queryAll(Map<String,Object> map, Pageable pageable) {
        IPage<FtLeaveMessageDto> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FtLeaveMessageDto> page = ftLeaveMessageMapper.getLeaveMessages(queryPage, map);
        return ConvertUtil.convertPage(page, FtLeaveMessageDto.class);
    }

    @Override
    public PageInfo<FtLeaveMessageDto> getScheduledService(Map<String, Object> map, Pageable pageable) {
        IPage<FtLeaveMessageDto> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FtLeaveMessageDto> page = ftLeaveMessageMapper.getScheduledService(queryPage, map);
        return ConvertUtil.convertPage(page, FtLeaveMessageDto.class);
    }

    @Override
    public List<FtLeaveMessageDto> downLoadData(FtLeaveMessageQueryParam ftLeaveMessageQueryParam) {
        return ftLeaveMessageMapper.download(ftLeaveMessageQueryParam);
    }

    @Override
    public List<FtLeaveMessageDto> queryAll(FtLeaveMessageQueryParam query){
        return ConvertUtil.convertList(ftLeaveMessageMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FtLeaveMessageDto.class);
    }

    @Override
    public FtLeaveMessage getById(String id) {
        return ftLeaveMessageMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FtLeaveMessageDto findById(String id) {
        return ConvertUtil.convert(getById(id), FtLeaveMessageDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FtLeaveMessageDto resources) {
        FtLeaveMessage entity = ConvertUtil.convert(resources, FtLeaveMessage.class);
        entity.setCreateTime(new Date());
        int leaveMessageInsertFlag = ftLeaveMessageMapper.insert(entity);
        FtOrder ftOrder = new FtOrder();
        ftOrder.setLeaveMessageId(entity.getId());
        ftOrder.setUserId(resources.getUserId());
        ftOrder.setOrderTime(new Date());
        if (leaveMessageInsertFlag > 0){
            //添加订单表
            int orderInsertFlag = ftOrderMapper.insert(ftOrder);
            if (orderInsertFlag > 0){
                FtOrderSchedule ftOrderSchedule = new FtOrderSchedule();
                ftOrderSchedule.setOrderId(ftOrder.getId());
                ftOrderSchedule.setUpdateTime(new Date());
                //添加订单进度表
                ftOrderMapper.insertOrderSchedule(ftOrderSchedule);
            }
        }
        return leaveMessageInsertFlag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FtLeaveMessageDto resources){
        FtLeaveMessage entity = ConvertUtil.convert(resources, FtLeaveMessage.class);
        int ret = ftLeaveMessageMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return ftLeaveMessageMapper.deleteBatchIds(ids);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    @Override
    public void download(List<FtLeaveMessageDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FtLeaveMessageDto ftLeaveMessage : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("所属服务", ftLeaveMessage.getServiceTitle());
              map.put("客户姓名", ftLeaveMessage.getUserName());
              map.put("联系方式", ftLeaveMessage.getContactNumber());
              map.put("预定时间", ftLeaveMessage.getCreateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }

    @Override
    public void downloadLeaveMessage(List<FtLeaveMessageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FtLeaveMessageDto ftLeaveMessage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("所属服务", ftLeaveMessage.getServiceTitle());
            map.put("客户姓名", ftLeaveMessage.getUserName());
            map.put("联系方式", ftLeaveMessage.getContactNumber());
            map.put("留言内容", ftLeaveMessage.getContent());
            map.put("留言时间", ftLeaveMessage.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
